package cn.liyunqi.ttv.main.action;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.ttv.main.localdb.ConfigHelper;
import cn.liyunqi.ttv.main.util.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class TTVActionUtil {

    public static boolean isWindows() {
        return System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1;
    }
    private static boolean devflg = true;


    public static JSONObject getApiParam() {
        try {

            if(devflg){
                String json = "{\n" +
                        "    \"MESSAGE\": \"\",\n" +
                        "    \"STATUS\": \"0000\",\n" +
                        "    \"Baidu_API_KEY\": \"zT2SXTHZZ9eB9aMDaBNnsiaz\",\n" +
                        "    \"Baidu_SECRET_KEY\": \"FCnSnlDuKG9A0P5kA3gLqWdz3goXvgDs\",\n" +
                        "    \"Baidu_APP_ID\": \"11386622\",\n" +
                        "    \"Kedaxunfei_APP_ID\": \"5b2e6eeb\"\n" +
                        "}";
                return JSONObject.parseObject(json);
            }

            String url = ConfigHelper.getloadValue("url");
            String result  = HttpUtil.sendGet(url + "/common/getApiParam",null);

            JSONObject rejson = JSONObject.parseObject(result);
            if("0000".equals(rejson.get("STATUS"))){
                return rejson;
            }else{
                throw new BizException(rejson.get("MESSAGE").toString());
            }
        } catch (Exception e) {

            throw new BizException("系统异常,无法连接到服务器");
        }
    }

    //simplifiedFlg?1:0,traditionalFlg
    public static void insertHistory(String userId, String text, String path, String fileName, int spd, int pit, int vol, String perId, int simplified, int traditional) {

        if(devflg){
            return ;
        }

        try {
            String url = ConfigHelper.getloadValue("url");
            Map<String,String> param = new HashMap<>();
            param.put("userId",userId);
            param.put("text",text);
            param.put("path",path);
            param.put("fileName",fileName);
            param.put("spd",spd+"");
            param.put("pit",pit+"");
            param.put("vol",vol+"");
            param.put("perId",perId);
            param.put("simplified",simplified+"");
            param.put("traditional",traditional+"");
            String result = HttpUtil.sendPost(url + "/common/checkText",param);

        } catch (Exception e) {

            //throw new BizException("系统异常");
        }
    }

    public static JSONObject checkAuth() {
        try {
            if(devflg){
                String json = "{\n" +
                        "    \"MESSAGE\": \"\",\n" +
                        "    \"STATUS\": \"0000\",\n" +
                        "    \"userId\": \"c3b629f2ff264849bcc5a57939da991f\",\n" +
                        "    \"token\": \"8471cdbe104f41dea78e30b48a677154\"\n" +
                        "}";
                return JSONObject.parseObject(json);
            }

            String url = ConfigHelper.getloadValue("url");
            String result  = HttpUtil.sendGet(url + "/user/checkAuth",null);

            JSONObject rejson = JSONObject.parseObject(result);
            return rejson;

        } catch (Exception e) {

            throw new BizException("系统异常,无法连接到服务器");
        }
    }
}
